<?php

/**
 +-----------------------------------------------------
 * PublicModel
 +-----------------------------------------------------
 * @author	liuricheng 
 * @time	2014-03-13
 * @type	[add]
 * 
 */
import("ORG.Util.Function");
import("@.Util.Utils");


class UserModel extends Model{
	
	private $users		=	"ybr_users";
	private $user_rank	=	"ybr_user_rank";
	
	public function get_sql_user_list(){
		
		/* 过滤条件 */
        $filter['keywords'] = empty($_REQUEST['keywords']) ? '' : trim($_REQUEST['keywords']);
        if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1){
            $filter['keywords'] = json_str_iconv($filter['keywords']);
        }
        $filter['rank'] = empty($_REQUEST['rank']) ? 0 : intval($_REQUEST['rank']);
        $filter['pay_points_gt'] = empty($_REQUEST['pay_points_gt']) ? 0 : intval($_REQUEST['pay_points_gt']);
        $filter['pay_points_lt'] = empty($_REQUEST['pay_points_lt']) ? 0 : intval($_REQUEST['pay_points_lt']);

        $filter['sort_by']    = empty($_REQUEST['sort_by'])    ? 'user_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC'     : trim($_REQUEST['sort_order']);

        $ex_where = ' WHERE 1 ';
        if ($filter['keywords']){
            $ex_where .= " AND user_name LIKE '%" . Utils::mysql_like_quote($filter['keywords']) ."%'";
        }
        if ($filter['rank']){
            $sql = "SELECT min_points, max_points, special_rank FROM ".$GLOBALS['ecs']->table('user_rank')." WHERE rank_id = '$filter[rank]'";
            $row = $GLOBALS['db']->getRow($sql);
            if ($row['special_rank'] > 0){
                /* 特殊等级 */
                $ex_where .= " AND user_rank = '$filter[rank]' ";
            }
            else{
                $ex_where .= " AND rank_points >= " . intval($row['min_points']) . " AND rank_points < " . intval($row['max_points']);
            }
        }
        if ($filter['pay_points_gt']){
             $ex_where .=" AND pay_points >= '$filter[pay_points_gt]' ";
        }
        if ($filter['pay_points_lt']){
            $ex_where .=" AND pay_points < '$filter[pay_points_lt]' ";
        }
        
        $sql = "SELECT user_id, user_name, email, is_validated, user_money, frozen_money, rank_points, pay_points, reg_time ".
                " FROM " . $this->users . $ex_where .
                " ORDER by " . $filter['sort_by'] . ' ' . $filter['sort_order'];
    	return $sql;
		
	}
	
	
	/**
     * 判断表中某字段是否重复，若重复则中止程序，并给出错误信息
     *
     * @access  public
     * @param   string  $col    字段名
     * @param   string  $name   字段值
     * @param   integer $id
     *
     * @return void
     */
    function is_only($col, $name, $id = 0, $where=''){
    	$CommonModel	=	new CommonModel();
        $sql 			= 	'SELECT COUNT(*) FROM ' .$this->user_rank. " WHERE $col = '$name'";
        $sql 			.=	empty($id) ? '' : ' AND ' . $this->id . " <> '$id'";
        $sql 			.= 	empty($where) ? '' : ' AND ' .$where;
        return ($CommonModel->getOne($sql) == 0);
    }
	
		
}

